package com.qf.listener;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Component
public class PreSmsListener {

    @Autowired
    FilterChainManager filterChainManager;

    @RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)
    //消费  队列 消息
    public void consum(StandardSubmit submit, Channel channel, Message message) {

        //开始处理消息: 如果执行策略出现问题， 会发送错误日志到 ES
        filterChainManager.execute(submit);

        //手动确认消息 消费成功（ack）
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
